package com.ruoyi.lendmoney.service.impl;

import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.lendmoney.domain.Agreement;
import com.ruoyi.lendmoney.domain.LoanCheckInfo;
import com.ruoyi.lendmoney.domain.LoanPersonTable;
import com.ruoyi.lendmoney.domain.Results;
import com.ruoyi.lendmoney.feign.AgreementFeign;
import com.ruoyi.lendmoney.feign.LoanCherckInfoFeign;
import com.ruoyi.lendmoney.mapper.LoanPersonTableMapper;
import com.ruoyi.lendmoney.service.LoanPersonTableService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class LoanPersonTableServiceImpl implements LoanPersonTableService {
    @Autowired
    private LoanPersonTableMapper loanPersonTableMapper;
    @Autowired
    private LoanCherckInfoFeign loanCherckInfoFeign;
    @Autowired
    private AgreementFeign agreementFeign;
    @Override
    public Results add(LoanPersonTable loanPersonTable) {
        Agreement agreementCode = agreementFeign.getAgreementCode(loanPersonTable.getUserCode());
        if (agreementCode==null){
            return new Results(500,"请先申请额度");
        }else if (agreementCode.getNum()<loanPersonTable.getDownPayment()) {
            return new Results(500,"额度不够,无法申请");
        }else {
            //随机字符串为code标识
            loanPersonTable.setApplyForLoanCoded(UUID.randomUUID().toString());
            //创建时间
            loanPersonTable.setCreateTime(LocalDateTime.now());
            loanPersonTable.setUpdateTime(LocalDateTime.now());
            loanPersonTable.setIsDelete(1);
            //添加贷款表
            loanPersonTableMapper.add(loanPersonTable);
            LoanCheckInfo loanCheckInfo = new LoanCheckInfo();
            BeanUtils.copyProperties(loanPersonTable, loanCheckInfo);
            //贷款表标识
            loanCheckInfo.setApplyForLoanCode(loanPersonTable.getApplyForLoanCoded());
            loanCheckInfo.setUserCode(loanPersonTable.getUserCode());
            loanCheckInfo.setUsername(loanPersonTable.getUsername());
            //车辆原价
            loanCheckInfo.setCarPrice(loanPersonTable.getBorrowingBalance());
            //默认为待审核
            loanCheckInfo.setIsStatus(3);
            loanCheckInfo.setIsDeleted(1);
            loanCheckInfo.setDownMoney(loanPersonTable.getDownMoney());
            //添加审核表
            loanCherckInfoFeign.add(loanCheckInfo);
            return new Results(200,"添加成功");
        }
    }
}
